import express from 'express';
import traceCtrl from '../controllers/traceCtrl';
import errorsCtrl from '../controllers/errorsCtrl';
import PerformanceCtrl from '../controllers/performanceCtrl';
import UnhandledrejectionCtrl from '../controllers/unhandledrejectionCtrl';
import ResourcesCtrl from '../controllers/resourcesCtrl';
import url from 'url';
const router = express.Router();

// 埋点
router.get('/log/trace.gif', (req, res) => {
  const queryData = url.parse(req.url, true)?.query || {};
  let type = Object.keys(queryData)?.[0];
  // todo: 这段代码 应该怎么写优雅 保证后面再添加别的类型 不用很多 if  else 一坨
  if (type === 'performance') {
    PerformanceCtrl.savePerformance(req, res);
  } else if (type === 'resources') {
    ResourcesCtrl.saveResources(req, res);
  } else if (type === 'error') {
    errorsCtrl.saveErrors(req, res);
  } else if (type === 'unhandledrejection') {
    UnhandledrejectionCtrl.saveUnhandledrejection(req, res);
  }
});

// 查询 errors 表中数据
router.post('/log/gerErrorlist', (req, res) => {
  errorsCtrl.getErrorList(req, res);
});

// 查询 errors 表中数据 *(分页)
router.post('/log/gerErrorByPage', (req, res) => {
  errorsCtrl.getErrorByPage(req, res);
});

export default router;
